﻿@inject IOptionsMonitor<WebsiteOptions> WebsiteOption

<div>
    <div class="row g-3">
        <div class="col-6 col-sm-4 col-md-3 col-xl-2">
            <Dropdown TValue="string" Items="Items" OnSelectedItemChanged="@ChangeURL" />
        </div>
        <div class="col-6 col-sm-4 col-md-3 col-xl-5">
            <BootstrapInput @bind-Value="@Url" />
        </div>
        <div class="col-6 col-sm-4 col-md-3 col-xl-auto">
            <Button Color="Color.Primary" OnClick="Apply">加载</Button>
        </div>
    </div>

    <FileViewer @ref="fileViewer" Filename="@CombineFilename(Url)" Height="300px" StyleString="padding: var(--bs-card-cap-padding-y) var(--bs-card-cap-padding-x);border: var(--bs-card-border-width) solid var(--bs-card-border-color);" />
</div>

@code {
    [NotNull]
    private string? Url { get; set; }

    [NotNull]
    private FileViewer? fileViewer { get; set; }

    [NotNull]
    private List<SelectedItem>? Items { get; set; }

    private List<string> FileList { get; } = new List<string>();
    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        FileList.Add("sample.xlsx");
        FileList.Add("sample2.xlsx");
        FileList.Add("sample3.xlsx");
        FileList.Add("sample2.docx");
        FileList.Add("sample.docx");
        Url = FileList[0];

        Items = FileList.Select(i => new SelectedItem(i, i)).ToList();
    }

    private string CombineFilename(string filename)
    {
#if DEBUG
        filename = Path.Combine(WebsiteOption.CurrentValue.WebRootPath, "samples", filename);
#else
        filename = Path.Combine(WebsiteOption.CurrentValue.ContentRootPath, "wwwroot", "samples", filename);
#endif
        return filename;
    }

    private async Task ChangeURL(SelectedItem e)
    {
        Url = e.Value;
        StateHasChanged();
        await fileViewer.Reload(CombineFilename(e.Value));
    }

    private async Task Apply()
    {
        await fileViewer.Reload(CombineFilename(Url));
    }
}
